package com.ruboweb.ui.pivot;

import java.io.FileInputStream;
import java.net.URL;

import org.apache.pivot.beans.BXML;
import org.apache.pivot.beans.BXMLSerializer;
import org.apache.pivot.beans.Bindable;
import org.apache.pivot.collections.ArrayList;
import org.apache.pivot.collections.Map;
import org.apache.pivot.collections.Sequence;
import org.apache.pivot.util.Resources;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.TreeView;
import org.apache.pivot.wtk.Window;
import org.apache.pivot.xml.Element;
import org.apache.pivot.xml.XMLSerializer;

public class GenerateReportWindow extends Window implements Application,
		Bindable {

	private Window window;

	@BXML
	private TreeView treeView = null;

	public GenerateReportWindow() {

	}

	@Override
	public void initialize(final Map<String, Object> namespace,
			final URL location, final Resources resources) {

	}

	@Override
	public void startup(final Display display,
			final Map<String, String> properties) throws Exception {
		BXMLSerializer bxmlSerializer = new BXMLSerializer();
		this.window = (Window) bxmlSerializer.readObject(StratUI.class,
				"/com/ruboweb/ui/generate_report_window.bxml");

		bxmlSerializer.bind(this);
		//this.cargarXMLConfig("config.xml");
		this.window.open(display);
	}

	private void cargarXMLConfig(String fichero) {
		XMLSerializer xmlSerializer = new XMLSerializer();
		FileInputStream fileInputStream = null;

		try {
			try {
				fileInputStream = new FileInputStream(fichero);
				ArrayList<Element> treeData = new ArrayList<Element>();
				treeData.add(xmlSerializer.readObject(fileInputStream));

				treeView.setTreeData(treeData);

				Sequence.Tree.Path path = new Sequence.Tree.Path(0);
				treeView.expandBranch(path);
				treeView.setSelectedPath(path);
			} finally {
				if (fileInputStream != null) {
					fileInputStream.close();
				}
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}

	}

	@Override
	public boolean shutdown(final boolean optional) throws Exception {
		if (this.window != null) {
			this.window.close();
		}

		return false;
	}

	@Override
	public void suspend() throws Exception {
	}

	@Override
	public void resume() throws Exception {
	}

	@Override
	public void open(Display display, Window owner) {
		super.open(display, owner);

	}

}
